package com.malphite.mongodemo.mongo;

import com.mongodb.bulk.BulkWriteResult;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.*;
import org.bson.Document;

import java.util.Arrays;

public class Mongo2Bulk {

    public static void main(String[] args) {

        MongoDatabase test = Mongo1Connection.getDataBase("test");
        MongoCollection<Document> collection = test.getCollection("bulk");

        BulkWriteResult result = collection.bulkWrite(
                    Arrays.asList(
                            new InsertOneModel<>(new Document("name", "A Sample Movie")),
                            new InsertOneModel<>(new Document("name", "Another Sample Movie")),
                            new InsertOneModel<>(new Document("name", "Yet Another Sample Movie")),
                            new UpdateOneModel<>(new Document("name", "A Sample Movie"),
                                    new Document("$set", new Document("name", "An Old Sample Movie")),
                                    new UpdateOptions().upsert(true)),
                            new DeleteOneModel<>(new Document("name", "Yet Another Sample Movie")),
                            new ReplaceOneModel<>(new Document("name", "Yet Another Sample Movie"),
                                    new Document("name", "The Other Sample Movie").append("runtime","42"))
                    ));
            System.out.println("Result statistics:" +
                    "\ninserted: " + result.getInsertedCount() +
                    "\nupdated: " + result.getModifiedCount() +
                    "\ndeleted: " + result.getDeletedCount());
    }
}
